package com.ixonos.cloud;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageButton;

public class MyImgButton extends ImageButton {
    private static final String TAG = "Weather";
    private Animation mAniRotate = null;
    private int mBitmapIds[] = { R.drawable.refresh_0, R.drawable.refresh_1, R.drawable.refresh_2,
            R.drawable.refresh_3, R.drawable.refresh_4, R.drawable.refresh_5, R.drawable.refresh_6,
            R.drawable.refresh_7, R.drawable.refresh_8, R.drawable.refresh_9, R.drawable.refresh_10,
            R.drawable.refresh_11 };
    private Bitmap mBitmaps[] = null;
    private Paint mPaint = null;
    private int mImgIndex = 0;
    private Thread mThread = null;

    public MyImgButton(Context context, AttributeSet attrs) {
        super(context, attrs, 0);

        mPaint = new Paint();
        mBitmaps = new Bitmap[mBitmapIds.length];
        for (int i = 0; i < mBitmapIds.length; i++) {
            Log.i("vtest", "mBitmaps load:" + i);
            mBitmaps[i] = ((BitmapDrawable) getResources().getDrawable(mBitmapIds[i])).getBitmap();
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        if (event.getAction() == MotionEvent.ACTION_UP) {
            if (mThread != null) {
                return true;
            }

            mThread = new Thread(new Runnable() {

                @Override
                public void run() {
                    int count = 0;

                    while (mThread != null) {
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                        ++ mImgIndex;
                        if (mImgIndex >= mBitmapIds.length) {
                            mImgIndex = 0;
                        }

                        postInvalidate();

                        ++ count;
                        if (count >= 24) {
                            mImgIndex = 0;
                            break;
                        }
                    }

                    mThread = null;
                }
            });

            mThread.start();
        }

        return super.onTouchEvent(event);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // super.onDraw(canvas);

        if (mBitmaps[mImgIndex] != null) {
            canvas.drawBitmap(mBitmaps[mImgIndex], 0, 0, mPaint);
        }
    }

    public void startAnimation() {
        mAniRotate = new RotateAnimation(0.0f, 360.0f * 2, Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f);
        mAniRotate.setDuration(2000);
        startAnimation(mAniRotate);
    }

    public void stopAnimation() {
        if (mAniRotate != null) {
            clearAnimation();
        }
    }

}
